<?php

namespace App\Http\Controllers\admin;
use Hash;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function getIndex(Request $request)
    {
        //查询所有的数据
        // $users = DB::table('users')->get();
        // dd($request);
        //获取每页显示几条
        $num = $request->input('num',10);
        //判断是否有查询语句
        if($request->input('keywords')){
              $users = DB::table('shop_users')->where('uname','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
              $users = DB::table('shop_users')->paginate($num);
        }

        //获取参数列表
        $list = $request->all();
        // dd($list);

        //解析模板
        return view('Admin.user.index',['users'=>$users,'list'=>$list]);
    }
    //后台添加页面
    public function getAdd()
    {
        //解析模板
        return view('admin.user.add');

    }   //进行数据添加

    public function postInsert(UserInsertRequest $request)
    {
        // dd($request->all());
        //执行数据验证
        // if(!$request->input('uname')){
        //     return back();
        // }

        //laravel内置验证方式 有闪存的功能
        // $this->validate($request,[
        //     'uname' => 'required',
        //     'email' => 'required',
        // ],[
        //     'uname.required' => '用户名不能为空',
        //     'email.required' => '邮箱不能为空',
        // ]);
       
       //提取数据
  
        $data = $request->only(['uname','upwd','email','tel']);
       

        //处理token
        $data['token'] = str_random(50);

        //密码加密
        $data['upwd'] = Hash::make($data['upwd']);

        //调用函数处理头像上传
        $data['uface'] = $this->upload($request,'uface');

        //执行数据插入
        $res = DB::table('shop_users')->insert($data);
        if($res){
              //成功
              return redirect('/admin/user/index')->with('success','用户添加成功');
        }else{
              return back()->with('error','用户添加失败');
        }
    }

    //封装方法 进行头像上传
    private function upload($request,$filename)
    {
         if($request->hasFile($filename)){
            //获取文件后缀名
            $suffix = $request->file($filename)->getClientOriginalExtension();
            // dd($suffix);
            //随机文件名
            $name = md5(time().rand(1,9999));
            $request->file($filename)->move('./uploads',$name.'.'.$suffix);
            return '/uploads/'.$name.'.'.$suffix;
         }
    }

    public function postDelete(Request $request)
    {
        // dd('22');
        //在$request里找到指定属性值
        $id = $request->input('id');
        //删除用户
        $res = DB::table('shop_users')->delete($id);
        echo $res;

    }

    //封装方法进行修改显示
    public function getEdit(Request $request)
    {
        
        $id = $request->input('id');
        //查询这个用户
        $users = DB::table('shop_users')->where('id',$id)->first();
         // dd($users);

        //解析模板
        return view('admin/user/edit',['users'=>$users]);
    }

    //数据修改
    public function postUpdate(Request $request)
    {
         $data = $request->only(['uname','upwd','email','tel']);

         //判断是否有头像上传
         if($request->hasFile('uface')){
              $data['uface'] = $this->upload($request,'uface');
         }
         //获取id
         $id = $request->input('id');
         // dd($id);

         //进行数据修改操作
         $res = DB::table('shop_users')->where('id',$id)->update($data);

         if($res){
               //成功 到列表页
              return redirect('/admin/user/index')->with('success','用户修改成功');
         }else{
              return back()->with('error','用户修改失败');
         }
    }
    
}
